package org.fujene.structure;

public final class NodeInfo implements Comparable<NodeInfo> {
	private String addr;
	private long hash;
	private boolean indexable;
	private boolean searchable;
	private boolean isDead;

	// 0x0 ～ 0xFF...FFで調べるからこんなことになる
	@Override
	public int compareTo(NodeInfo arg) {
		// return (arg.hash - this.hash > 0 ? -1 : 1);
		if (this.hash >= 0) {
			if (arg.hash < 0)
				return -1; // argのほうが大きい
			else
				return (this.hash > arg.hash ? 1 : -1);
		} else {
			if (arg.hash >= 0)
				return 1; // argのほうが小さい
			else
				return (this.hash > arg.hash ? 1 : -1);
		}
	}

	public boolean equals(Object arg) {
		if (arg instanceof NodeInfo) {
			return ((NodeInfo) arg).hash == this.hash;
		}

		return false;
	}

	public NodeInfo(String addr, long hash) {
		this.addr = addr;
		this.hash = hash;
		this.isDead = false;
		this.indexable = false;
		this.searchable = false;
	}

	public String getAddr() {
		return addr;
	}

	public long getHash() {
		return hash;
	}

	public boolean isDead() {
		return isDead;
	}

	public void indexable() {
		this.indexable = true;
	}

	public boolean isIndexable() {
		return indexable;
	}

	public void searchable() {
		this.searchable = true;
	}

	public boolean isSearchable() {
		return searchable;
	}

	public void setDead() {
		this.isDead = true;
	}

	@Override
	public String toString() {
		return String.format("<%s, 0x%016x>", addr, hash);
	}
}
